<template>
  <el-card>
    <template #header>
      <h4>商品编辑</h4>
    </template>
    <div>
      <el-form ref="form" :model="comForm" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="comForm.name" class="ww"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="comForm.category" placeholder="请选择商品分类">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.cateName"
              :value="item.cateName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input-number
            v-model="comForm.price"
            @change="handleChange"
            :min="0.1"
            :max="99999999"
            label="描述文字"
          ></el-input-number>
        </el-form-item>

        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://172.16.100.203:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="商品描述">
          <el-input
            class="ww"
            type="textarea"
            v-model="comForm.goodsDesc"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认修改 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { comEdit, getcate, comList } from '../../api/info.js'
export default {
  data() {
    return {
      comForm: {
        name: '121',
        category: '',
        price: 1,
        imgUrl: '',
        goodsDesc: '',
        id: ''
      },
      imageUrl: '',
      cateList: []
    }
  },
  created() {
    this.comForm.id = this.$route.params.id
    this.getcate()
    this.getList()
  },
  methods: {
    // 获取分类
    async getcate() {
      const { categories } = await getcate({
        currentPage: 1,
        pageSize: 999
      })
      console.log(categories)
      this.cateList = categories
    },

    // 获取所有数据再过滤
    async getList() {
      const { data } = await comList({
        currentPage: 1,
        pageSize: 999999
      })
      console.log(data)
      console.log(this.comForm.id)
      // 通过id把那一行过滤出来
      const comForm = data.filter(item => item.id === Number(this.comForm.id))
      this.comForm.name = comForm[0].name
      this.comForm.category = comForm[0].category
      this.comForm.price = comForm[0].price
      this.comForm.imgUrl = comForm[0].imgUrl
      this.comForm.goodsDesc = comForm[0].goodsDesc
      this.imageUrl =
        'http://127.0.0.1:5000/upload/imgs/goods_img/' + comForm[0].imgUrl
      console.log(comForm[0])
    },

    // 修改
    async onSubmit() {
      if (
        this.comForm.name &&
        this.comForm.category &&
        this.comForm.imgUrl &&
        this.comForm.goodsDesc
      ) {
        const data = await comEdit(this.comForm)
        console.log(data)
        if (data.code === 0) {
          this.$router.push('/pro/list')
        } else {
          this.$message.error('添加失败')
        }
      } else {
        this.$message.error('请将商品信息填写完整')
      }
    },

    // 计数器
    handleChange(value) {
      this.comForm.price = value
    },

    // 图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.comForm.imgUrl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
}
.avatar {
  width: 140px;
  height: 140px;
  display: block;
}

.ww {
  width: 400px;
}
</style>
